<?php

namespace App\Http\Controllers\Client\Course;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LessonDetailController extends ClientController
{
    /**
     * 查询课程详情，课程内容
     */
    public function index()
    {

        $all = $this->all;
        if (!isset($all['lesson_id'])) {
            return $this->setReturnJson(-1, '未传参数');
        }

        try {
            // 验证是否必须购买课程
            $need_to_buy = $this->needToBuy($all['lesson_id']);
            if ($need_to_buy) {
                return $this->setReturnJson(-1, '您尚未购买该课程');
            }

            // 记录学习记录
            $learned = DB::table('laifei_lessons')->where('we_id','=',$this->we_id)
                ->where('lesson_id','=',$all['lesson_id'])
                ->count();
            if(!$learned){
                DB::table('laifei_lessons')->insert([
                    'we_id'=>$this->we_id,
                    'lesson_id'=>$all['lesson_id'],
                ]);
            }
            // 查询课程资源内容
            $source = $this->lessonSources($all['lesson_id']);

            // 查询课程文本内容
            $contents = $this->lessonContents($all['lesson_id']);

            return $this->setReturnJson(0, 'success', ['source' => $source, 'contents' => $contents]);
        } catch (\Exception $e) {
            Log::error(LessonDetailController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '课程查询失败');
        }

    }

    /**
     * 验证用户是否已经购买了课程
     * @param int $lesson_id
     * @return bool
     */
    public function needToBuy(int $lesson_id): bool
    {
        $course_id = DB::table('laifei_sys_lessons')
            ->where('lesson_id', '=', $lesson_id)
            ->value('course_id');

        // 查询课程价格，如果免费直接放行
        $price = DB::table('laifei_sys_course')->where('course_id','=',$course_id)->value('price');
        if($price == 0){
            return false;
        }

        // 查询用户是否购买了课程,未购买返回true
        $bought = DB::table('laifei_course')
            ->where('course_id','=',$course_id)
            ->where('we_id','=',$this->we_id)
            ->count();
        return $bought? false: true;
    }

    /**
     * 查询课程资源内容
     * @param int $lesson_id
     * @return array
     */
    public function lessonSources(int $lesson_id): array
    {
        $result = DB::table('laifei_sys_lessons')
            ->where('lesson_id', '=', $lesson_id)
            ->first();
        $result = json_decode(json_encode($result), true);
        $result['url'] = AliOss::getUrl($result['url']);
        return $result;
    }


    /**
     * 查询课程文本内容
     * @param int $lesson_id
     * @return array
     */
    public function lessonContents(int $lesson_id): array
    {
        $result = DB::table('laifei_sys_lessons_sections')
            ->where('lesson_id', '=', $lesson_id)
            ->select('type', 'content')
            ->get()
            ->map(function ($item) {
                if ($item->type === 2) {
                    $item->content = AliOss::getUrl($item->content);
                }
                return $item;
            })
            ->toArray();

        return $result;
    }


}